package models

import "github.com/astaxie/beego/orm"

type Menu struct {
	Id int			`json:"id"`
	Pid int			`json:"pid"`
	Name string 	`json:"name"`
	Sort int 		`json:"sort"`
	Route string	`json:"route"`
}

type TreeList struct {
	Id int			`json:"id"`
	Name string		`json:"name"`
	Pid int			`json:"pid"`
	Sort int 		`json:"sort"`
	Route string	`json:"route"`
	Children []*TreeList	`json:"children"`
}

func init()  {
	// 注册模型
	//orm.RegisterModel(new(Menu))
}

func (m *Menu)TableName() string {
	return "auth_menu"
}

/**
菜单列表
*/
func (m *Menu) MenuList() []*TreeList{
	return m.getMenu(0)
}

/**
递归获取树形菜单
*/
func (m *Menu) getMenu(pid int) []*TreeList {
	o := orm.NewOrm()
	var menu []Menu
	_,_ = o.QueryTable("auth_menu").Filter("pid", pid).OrderBy("sort").All(&menu)
	treeList := []*TreeList{}
	for _, v := range menu{
		child := v.getMenu(v.Id)
		node := &TreeList{
			Id:v.Id,
			Name:v.Name,
			Sort:v.Sort,
			Route:v.Route,
			Pid:v.Pid,
		}
		node.Children = child
		treeList = append(treeList, node)
	}
	return treeList
}
